<?php

namespace addons\wxfollowlogin;


use think\Addons;
use think\Request;

/**
 * 插件
 */
class Wxfollowlogin extends Addons
{
    protected static $html = ['register' => ''];
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {

        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {

        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {

        return true;
    }

    /**
     * 模块开始
     */
    public function moduleInit()
    {
        $request = Request::instance();

        $module = strtolower($request->module());
        $controller = strtolower($request->controller());
        $action = strtolower($request->action());
        if ($module !== 'index' || $controller !== 'user' || !in_array($action, ['login', 'register'])) {
            return;
        }
        $url = $request->get('url', $request->server('HTTP_REFERER', '', 'trim'), 'trim');
        $data = [
            'url'    => $url
        ];
        self::$html['register'] = $this->view->fetch('view/hook/buttons', $data);
    }



    /**
     * HTML替换
     */
    public function viewFilter(& $content)
    {
        $request = Request::instance();
        $module = strtolower($request->module());
        $controller = strtolower($request->controller());
        $action = strtolower($request->action());
        if ($module !== 'index' || $controller !== 'user') {
            return;
        }

        if (in_array($action, ['login', 'register'])) {
            $html = self::$html['register'] ?? '';
            $content = str_replace("<!--@IndexRegisterFormEnd-->", "<!--@IndexRegisterFormEnd-->".$html, $content);
            $content = str_replace("<!--@IndexLoginFormEnd-->", "<!--@IndexLoginFormEnd-->".$html, $content);
        }
    }

}
